#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <mysql.h>

typedef int(*HANDLE_SQL_CB)(MYSQL_RES *result,int order,void *data,int size);

int print_sql_result(MYSQL_RES *result,int order,void *data,int size)
{
	int col_num = 0,row_num = 0,i = 0;
	MYSQL_ROW row;
	col_num = mysql_num_fields(result);
	row_num = mysql_num_rows(result);
	printf("---fields num = %d,row num = %d,order = %d\n",col_num,row_num,order);
	while ((row = mysql_fetch_row(result)))
	{// null indicates there no more row.
		unsigned long *lengths;
		lengths = mysql_fetch_lengths(result);
		for(i = 0; i < col_num; i++)
		{
			printf("[%.*s] ", (int) lengths[i], row[i] ? row[i] : "NULL");
		}
		printf("\n");
	}
	return 0;
}

int mysql_cmd_handle(MYSQL *myconn,const char *sql,HANDLE_SQL_CB func,void *data,int size)
{
	int col_num = 0,row_num = 0;
	int i, res_num = 0;
	MYSQL_RES *result;

	if(mysql_query(myconn,sql))
	{
		fprintf(stderr,"mysql_query fail:%s\n",mysql_error(myconn));
		return -1;
	}
	do{
		res_num++;
		//mysql_use_result(); // row-by-row get result from server
		result = mysql_store_result(myconn);
		if(result)
		{
			col_num = mysql_num_fields(result);
			row_num = mysql_num_rows(result);
			//printf("---fields num = %d,row num = %d\n",col_num,row_num);
			if(func)
			{
				func(result,res_num,data,size);
			}
			mysql_free_result(result);
		}
		else
		{
			if(mysql_field_count(myconn)==0)
			{
				// query does not return data
				// (it was not a SELECT)
				row_num = mysql_affected_rows(myconn);
				printf("---affected rows = %d\n",row_num);
			}
			else
			{
				// mysql_store_result() should have returned data
				fprintf(stderr, "Error: %s\n", mysql_error(myconn));
			}
		}
	}while(!mysql_next_result(myconn)); /* 0 - more results */
	printf("----result set number = %d\n",res_num);
	return 0;
}

int main(int argc,char *argv[])
{
	int ret;
	MYSQL *myconn;
	MYSQL_RES *result;

	printf("-----开始测试----\n");
	if(mysql_library_init(0,NULL,NULL))
	{
	fprintf(stderr,"mysql_library_init fail!\n");
	exit(EXIT_FAILURE);
	}

	myconn = mysql_init(NULL);
	if(myconn==NULL)
	{
	fprintf(stderr,"mysql_init fail!\n");
	exit(EXIT_FAILURE);
	}
	int reconnect = 1;
	char *charset = "utf8";
	mysql_options(myconn,MYSQL_OPT_RECONNECT,&reconnect);
	mysql_options(myconn,MYSQL_SET_CHARSET_NAME,charset);

	//mysql_errno();
	//if(mysql_real_connect(myconn,"localhost","dubhe","2014ipc","ipc",0,NULL,0)==NULL)
	if(mysql_real_connect(myconn,"119.147.144.102","dubhe","2014ipc","ipc",0,NULL,0)==NULL)
	{
		fprintf(stderr,"mysql_real_connect fail:%s\n",mysql_error(myconn));
		exit(EXIT_FAILURE);
	}

	// SQL statment
	//mysql_cmd_handle(myconn,"select * from account",print_sql_result,NULL,0);
	//mysql_cmd_handle(myconn,"call sp_query_user_devlist('15015600262',@list_num)",print_sql_result,NULL,0);
	//mysql_cmd_handle(myconn,"call sp_query_message_list('15015600262',7)",print_sql_result,NULL,0);
	//mysql_cmd_handle(myconn,"call sp_query_test",print_sql_result,NULL,0);
	//mysql_cmd_handle(myconn,"call sp_add_user_friend('11111111117','01234567890','friend')",print_sql_result,NULL,0);
	//mysql_cmd_handle(myconn,"call sp_query_event_history('MEIA-000073-PGLCB', '2015-5-12 00:00:00', '2015-05-26 23:00:00', '%%Alarm', 2, 10)",print_sql_result,NULL,0);
	int i = 0;
	char userid[16];
	char username[32];
	char pwd[16];
	char type[16];
	char sqlcmd[1024]={0};
	for(i=1000;i<=10000;i++)
	{
	    snprintf(userid,sizeof(userid),"%011d",i);
	    snprintf(username,sizeof(username),"test%04d\n",i);
	    strcpy(pwd,"123456");
	    strcpy(type,"test");
	    snprintf(sqlcmd,sizeof(sqlcmd),"insert into users(userid,username,passwd,type) values('%s','%s','%s','%s')",userid,username,pwd,type);
	    //printf("sqlcmd:%s\n",sqlcmd);
	    mysql_cmd_handle(myconn,sqlcmd,NULL,NULL,0);
	}

	mysql_close(myconn);
	mysql_library_end();

	return ret;
}

